﻿using MVVMLesson02.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace MVVMLesson02.ViewModel
{
    public class MainViewModel
    {
        public DataModel Data { get; set; }

        public Command ClickMeCommand { get; set; }

        public MainViewModel()
        {
            Data = new DataModel();
            #region 两种方式都可以
            //ClickMeCommand = new Command(DoClick, CanClick);
            ClickMeCommand = new Command(new Action<object?>(DoClick), new Func<object?, bool>(CanClick)); 
            #endregion

            Data.PropertyChanged += Value1_PropertyChanged;
        }

        private void Value1_PropertyChanged(object? sender, PropertyChangedEventArgs e)
        {
            if (new string[] { "Value1", "Value2" }.Contains(e.PropertyName))
            {
                ClickMeCommand.RaiseCanExecuteChanged();
            }
        }

        public void DoClick(object? parameter)
        {
            Data.Value3 = Data.Value2 + Data.Value1 + int.Parse(parameter.ToString());
        }

        public bool CanClick(object? parameter)
        {
            // TODO: Add code to check if the button can be clicked.
            if (Data.Value1 > 0 && Data.Value2 > 0)
            {
                return true;
            }
            else 
            {
                return false;
            }
        }
    }
}
